<?php

class SectionsModule extends fvModule {

    private $appName = 'frontend';
    private $appConfig = null;
    
    function __construct () {
        $this->appConfig = new fvConfig(fvSite::$fvConfig->get("path.application.{$this->appName}.config"));
        $this->appConfig->Load("modules.yml");
        $this->appConfig->Load("template.yml");
        
        $moduleName = strtolower(substr(__CLASS__, 0, -6));

        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showIndex() {
        if (!fvRequest::getInstance()->hasRequestParameter("page")) {
            if (fvSite::$fvSession->is_set(fvRequest::getInstance()->getRequestParameter("requestURL")."/page")) {
                fvRequest::getInstance()->putRequestParameter("page",
                    (int)fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/page"));
            } 
        } else {
            fvSite::$fvSession->set(fvRequest::getInstance()->getRequestParameter("requestURL")."/page", 
                fvRequest::getInstance()->getRequestParameter("page"));
        }

        if (substr($requestURL = fvRequest::getInstance()->getRequestParameter("requestURL"), 0, 1) == "/") {
            $requestURL = substr($requestURL, 1);
        }
        
        $SectionManager = SectionsManager::getInstance();
//        $pager = new fvPager(SectionsManager::getInstance());
		$fvTree = new fvTree($SectionManager->getSectionTree());
        $this->__assign('Sections', $fvTree->return_tree());
//        $this->__assign('Sections', $pager->paginate(null, "IF (parent_id = 0, id*100000, parent_id*100000 + id)"));
        $request = fvRequest::getInstance();
        if (!$Section = SectionsManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Section = new Sections();
        }
        $this->__assign(array(
            'Section' => $Section,
            'SectionManager' => SectionsManager::getInstance(), 
        ));
        return $this->__display('section_list.tpl');
    }

}
